//
//  SAInventorySortView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/6.
//

#import "SAInventorySortView.h"

@interface SAInventorySortView ()

@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, strong) UIButton *currentBtn;

@end

@implementation SAInventorySortView

+(instancetype)shareCornView {
    SAInventorySortView * shareView = [[SAInventorySortView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.6);
    }
    return self;
}

-(void)loadData:(NSArray *)dataArr AndSelectNum:(NSInteger )num {
    self.dataArr = dataArr;
    self.num = num;
    [self initUI];
}

-(void)clickedBtn:(UIButton *)sender {
    if (sender != self.currentBtn) {
        sender.selected = YES;
        self.currentBtn.selected = NO;
        self.currentBtn = sender;
    }
    
    !self.selectBlock ?: self.selectBlock(self.currentBtn.tag - 100);
    [self shareDissTapEvent];
}

-(void)initUI {
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight- kScaleNum(45 * self.dataArr.count + SafeFooterHeight));
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight- kScaleNum(45 * self.dataArr.count + SafeFooterHeight), kScreenWidth, kScaleNum(45 * self.dataArr.count + SafeFooterHeight))];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];
    
    for (int i = 0; i < self.dataArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:self.dataArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [btn setTitleColor:kPinkColor forState:UIControlStateSelected];
        btn.titleLabel.font = kFont(16);
        btn.frame = SARectMake(0, 1 + 45 * i, kBaseScreenWidth, 45);
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:btn];

        UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 44 + 45 * i, kBaseScreenWidth, 1)];
        line.backgroundColor = kLineColor;
        [whiteView addSubview:line];
        
        if (i == self.num) {
            btn.selected = YES;
           self.currentBtn = btn;
        }else {
            btn.selected = NO;
        }
    }
}

- (void)showView:(UIViewController *)Sview {
    if(Sview == nil){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)shareDissTapEvent {
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)confirmationTapEvent {
    [UIView animateWithDuration:0.5f animations:^{
        
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}


@end
